import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/utils/assets.dart';
import 'package:fate_blind_box/utils/provider.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../network/Api.dart';
import '../../utils/snack_bar.dart';

//个人简介
class ModifyNickUI extends StatefulWidget {
  final String introduce;

  ModifyNickUI(this.introduce, {Key? key}) : super(key: key);

  @override
  State<ModifyNickUI> createState() => _ModifyNickUIState();
}

class _ModifyNickUIState extends State<ModifyNickUI> {

  final TextEditingController _introdceController = TextEditingController();


  int number = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }

  // 修改昵称
  _getUserinfo() async {

    final res = await Api.getInstance().ModifyNick(Provider.memberId,_introdceController.text);
    final data = res['msg'];

    ThemeSnackBar.show(context, data);
    Navigator.of(context).pop(true);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF9F9F9),

      appBar: AppBar(

        title: const Text('修改昵称',style: TextStyle(color: Colors.black,fontSize: 16),),
        elevation: 0,

        actions: <Widget>[
          Container(
            margin: const EdgeInsets.only(right: 15,top: 10,bottom: 10),
            height: 31,
            child: TextButton(
              style: TextButton.styleFrom(
                backgroundColor: ThemeColor.appColor,
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
                textStyle: const TextStyle(color: Colors.white),
              ),

              child: const Text('完成',style: TextStyle(color: Colors.white),),

              onPressed: () {
                _getUserinfo();
              },
            ),
          )


        ],
      ),

      body: Container(
          margin: const EdgeInsets.fromLTRB(10, 5, 10, 0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,

            children: [
              Container(
                  alignment: Alignment.topLeft,
                  child:Text(
                    '七天内可修改一次名字',
                    style: TextStyle(color: ThemeColor.textColorSecond,
                      fontSize: 16,
                    ),
                  )
              ),
              const SizedBox(height: 10,),
              Stack(

                children: [
                  Container(
                    color: Colors.white,
                    // height: 50,

                    alignment: Alignment.center,
                    child: TextField(

                      maxLines: 1,
                      maxLength: null,
                      scrollPadding: const EdgeInsets.all(20),
                        
                  onChanged: (text){

                        setState(() {
                          number = _introdceController.text.length;

                        });
                        print('----------');
                      },
                      controller: _introdceController,
                      style: const TextStyle(
                        fontSize: 16,
                        color: Colors.black,
                      ),

                      decoration:  InputDecoration(
                        hintText:widget.introduce?? "请修改昵称",

                        border: InputBorder.none,
                        contentPadding: EdgeInsets.all(5),
                      ),
                        inputFormatters: <TextInputFormatter>[LengthLimitingTextInputFormatter(16)],
                    ),
                  ),
                  Container(
                    // padding: EdgeInsets.only(right: 5),
                    height: 50,
                    margin: const EdgeInsets.symmetric(horizontal: 5),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children:[
                      Text('${number}' +'/16',style: TextStyle(color: Color(0xFFA7A7A7),fontSize: 16),),
                      ]
                    ),
                  )
                ],
              )
            ],
          )
      ),
    );
  }
}